package controller;

import java.io.File;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.Date;
import java.util.Iterator;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import bean.Fileinfo;
import service.FileService;
import service.FolderService;

@Controller
@RequestMapping("/file")
public class FileController {
	@Resource
	private FileService fileService;
	@Resource
	private FolderService folderService;
	
	@RequestMapping("/uploadfile")
	public @ResponseBody void UploadFile(HttpServletRequest request, HttpServletResponse response) 
			throws IllegalStateException, IOException{
		System.out.println("enter controller");
		//创建一个通用的多部分解析器 
				//System.out.println("here!");
		        CommonsMultipartResolver multipartResolver = 
		        		new CommonsMultipartResolver(request.getSession().getServletContext());  
		        String fileName = "";
		        HttpSession session = request.getSession();
		        //String userId = (String) session.getAttribute("userId");
		        //int folderId = Integer.parseInt((String) session.getAttribute("folderId"));
		        String userId="admin";
		        int folderId = 14;
		        
		        Fileinfo fileinfo = new Fileinfo();
		        fileinfo.setBelongTo(folderId);
		        fileinfo.setFilePath(folderService.FindById(folderId).getFolderPath());
		        fileinfo.setUploadUser(userId);
		        
		        //判断 request 是否有文件上传,即多部分请求  
		        if(multipartResolver.isMultipart(request)){
		            //转换成多部分request    
		            MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest)request;  
		            //取得request中的所有文件名  
		            Iterator<String> iter = multiRequest.getFileNames();  
		            while(iter.hasNext()){  
		                //记录上传过程起始时的时间，用来计算上传时间  
		                //int pre = (int) System.currentTimeMillis();  
		                //取得上传文件  
		                MultipartFile file = multiRequest.getFile(iter.next());  
		                if(file != null){  
		                    //取得当前上传文件的文件名称  
		                    String myFileName = file.getOriginalFilename();
		                    //如果名称不为“”,说明该文件存在，否则说明该文件不存在  
		                    if(myFileName.trim() !=""){  
		                        System.out.println(myFileName);  
		                        //重命名上传后的文件名  
		                        //String fileName = "demoUpload" + file.getOriginalFilename();  
		                        fileinfo.setFileName(file.getOriginalFilename());
		                        //定义上传路径  
		                        String path = fileinfo.getFilePath()+"/"+fileinfo.getFileName();
		                        File localFile = new File(path);  
		                        file.transferTo(localFile);  
		                        fileinfo.setUploadDatetime(new Timestamp(System.currentTimeMillis()));
		                    }
		                }
		                //记录上传该文件后的时间  
		                int finaltime = (int) System.currentTimeMillis();  
		                //System.out.println(finaltime - pre);  
		            }
		        }
	}
}
